package com.gpframework.module.purchase.service.impl;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.lang.TypeReference;
import com.gpframework.common.utils.StringUtils;
import com.gpframework.module.purchase.domain.GpPurchasingMakingDetails;
import com.gpframework.module.purchase.service.IGpPurchasingMakingDetailsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gpframework.module.purchase.mapper.GpProcureSchedulingMapper;
import com.gpframework.module.purchase.domain.GpProcureScheduling;
import com.gpframework.module.purchase.domain.vo.GpProcureSchedulingVo;
import com.gpframework.module.purchase.service.IGpProcureSchedulingService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
/**
 * @ClassName GpProcureScheduling
 * @Author gpframework
 * @Date 2023-12-22
 * @Version 1.0.0
 * @Description 采购排单Service业务层处理
 */
@Service
public class GpProcureSchedulingServiceImpl extends ServiceImpl<GpProcureSchedulingMapper, GpProcureScheduling> implements IGpProcureSchedulingService {

    @Autowired
    private IGpPurchasingMakingDetailsService iGpPurchasingMakingDetailsService;

    /**
     * @Description 查询列表返回VO 用于返回给前端的列表接口
     * @Author gpframework
     * @Date 2023-12-22
     * @Param [lqw]
     * @Return List<GpProcureSchedulingVo>
     */
    @Override
    public List<GpProcureSchedulingVo> listVo(QueryWrapper<GpProcureScheduling> lqw) {
        return this.baseMapper.listVo(lqw);
    }

    /**
     * @Description 通过查询详情VO 用于返回给前端的列详情接口
     * @Author gpframework
     * @Date 2023-12-22
     * @Param [id]
     * @Return GpProcureSchedulingVo
     */
    @Override
    public GpProcureSchedulingVo getVoById(Long id) {
        return this.baseMapper.getVo(id);
    }

    /**
     * 生成采购排单表
     * @param ids
     * @param createBy
     * @return
     */
    @Override
    public boolean createProcureScheduling(List<Long> ids, String createBy) {
        List<GpProcureScheduling> list = new ArrayList<>();
        for (Long id : ids) {
            GpPurchasingMakingDetails gpPurchasingMakingDetails = iGpPurchasingMakingDetailsService.getById(id);
            if (gpPurchasingMakingDetails == null) {
                continue;
            }
            GpProcureScheduling gpProcureScheduling = Convert.convert(new TypeReference<GpProcureScheduling>() {}, gpPurchasingMakingDetails);
            if (StringUtils.isNotBlank(createBy)) {
                gpProcureScheduling.setCreateBy(createBy);
            }
            gpProcureScheduling.setProductionApplicationId(gpPurchasingMakingDetails.getProductionApplicationId());
            gpProcureScheduling.setPurchasingDetailsId(gpPurchasingMakingDetails.getId());
            gpProcureScheduling.setOperatorTime(new Date());
            list.add(gpProcureScheduling);
        }
        return this.saveBatch(list);
    }

    @Override
    public boolean copy(Long id, Integer copyNum) {
        GpProcureScheduling gpProcureScheduling = this.getById(id);
        if (gpProcureScheduling == null) {
            return false;
        }
        for (int i = 0; i < copyNum; i++) {
            gpProcureScheduling.setId(null);
            this.save(gpProcureScheduling);
        }
        return true;
    }
}
